package cn.com.ntr.jhclient.adapter;

import java.util.List;
import java.util.Map;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import cn.com.ntr.jhclient.R;
import cn.com.ntr.jhclient.model.Position;

public class MyListAdapter extends BaseExpandableListAdapter {
	private LayoutInflater layoutInflater;
	List<Map<String, Position>> group;
	List<List<Map<String, Position>>> child;

	public MyListAdapter(Context context, List<Map<String, Position>> group,
			List<List<Map<String, Position>>> child) {
		layoutInflater = LayoutInflater.from(context);
		this.group = group;
		this.child = child;
	}

	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = layoutInflater.inflate(R.layout.layout_group_item,
					null);
		}
		Position group = (Position) getGroup(groupPosition);
		TextView groupName = (TextView) convertView
				.findViewById(R.id.tv_position);

		groupName.setText(group.getpName());
		ImageView arrow = (ImageView) convertView.findViewById(R.id.arrow_item);
		if (isExpanded) {
			
			arrow.setImageResource(R.drawable.arrow_item_down);

		} else {
			arrow.setImageResource(R.drawable.arrow_item);
		}
		return convertView;
	}

	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	public Object getGroup(int groupPosition) {
		return group.get(groupPosition).get("GROUP");
	}

	public int getGroupCount() {
		return group.size();

	}

	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = layoutInflater.inflate(R.layout.layout_child_item,
					null);
		}
		Position subPosition = (Position) getChild(groupPosition, childPosition);
		TextView sub = (TextView) convertView.findViewById(R.id.tv_sub_position);
		sub.setText(subPosition.getpName());
		return convertView;
	}

	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	public Object getChild(int groupPosition, int childPosition) {
		return child.get(groupPosition).get(childPosition).get("CHILD");
	}

	public int getChildrenCount(int groupPosition) {
		return child.get(groupPosition).size();
	}

	public boolean hasStableIds() {
		return true;
	}

	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}
}
